package com.lx.yygh.cmn.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lx.yygh.cmn.listener.UploadDataListener;
import com.lx.yygh.cmn.service.DictService;
import com.lx.yygh.common.result.Result;
import com.lx.yygh.model.cmn.Dict;
import com.lx.yygh.vo.cmn.DictEeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author c先生
 * @date 2022年10月11日 上午 11:28:30
 * @describe
 */
@Slf4j
@Api(tags = "数据字典模块")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin//支持跨域请求访问
public class DictController {
    @Resource
    DictService dictService;


    @ApiOperation(value = "展示所有的数据")
    @GetMapping("/findAll/{parentId}")
    public Result findAll(@PathVariable("parentId")Long parentId){
        List<Dict> list = dictService.findDictEeVo(parentId);
        return Result.ok(list);

    }

    @ApiOperation(value = "下载数据到本地")
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        //从数据库中查询所有的数据
        List<Dict> list = dictService.list();
        List<DictEeVo> voList = new ArrayList<>();
        //封装到DictEeVo中
        for (Dict dict : list) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo,DictEeVo.class);
            voList.add(dictEeVo);
        }

        String fileName = null;
        try {
            //把文件名编码格式进行修改，防止下载时的文件名是乱码
            fileName = URLEncoder.encode("医院系统数据字典","utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.addHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        response.addHeader("Access-Control-Expose-Headers", "Content-disposition");

        EasyExcel.write( response.getOutputStream(), DictEeVo.class).sheet("医院数据字典").doWrite(voList);

    }

    @ApiOperation(value = "从本地导入数据")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        dictService.upload(file);
        return Result.ok();
    }

    @ApiOperation(value = "根据dictcode和value获取数据字典值的名字")
    @GetMapping("/getName/{dictcode}/{value}")
    String getName(@PathVariable("dictcode") String dictcode,@PathVariable("value")String value){
        return dictService.getName(dictcode,value);
    }

    @ApiOperation(value = "根据value获取数据字典值的名字")
    @GetMapping("/getName/{value}")
    String getName(@PathVariable("value")String value){
        return dictService.getName("",value);
    }

    @ApiOperation(value = "获取省份")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode")String dictCode){
        List<Dict> dictList = dictService.getProvinceList(dictCode);
        return Result.ok(dictList);
    }

    @ApiOperation(value = "根据省份id获取市")
    @GetMapping("/findChildData/{provinceId}")
    public Result findChildData(@PathVariable("provinceId")Long provinceId){
        List<Dict> dictList = dictService.findDictEeVo(provinceId);
        return Result.ok(dictList);
    }
}
